package idv.takeshi.software.productline.bookstore.domain.model.order;

import idv.takeshi.software.productline.bookstore.domain.model.book.Book;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * A model represents as a OrderItem.
 * @author takeshi
 *
 */
@Entity
@Table(name = "ORDER_ITEM")
public class OrderItem {
 
	@Id
    @GeneratedValue(strategy = GenerationType.TABLE)
	private Long id;
	
	@Column(nullable = false)
	private int sequence;
	
	@Column(nullable = false, length = 50)
	@Enumerated(EnumType.STRING)
	private OrderItemStatus status;
	
	@Column(nullable = false)
	private int quantity;
	
	@Column(nullable = false, name = "UNIT_PRICE")
	private BigDecimal unitPrice;
	
	@Column(nullable = false, name = "UPDATE_TIME")
	private Date updateTime;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ORDERED_BOOK_ID")
	private Book orderedBook;
	 
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}
	
	/**
	 * @return the sequence
	 */
	public int getSequence() {
		return sequence;
	}

	/**
	 * @param sequence the sequence to set
	 */
	public void setSequence(int sequence) {
		this.sequence = sequence;
	}

	/**
	 * Check whether need replenishment by it's inventory status. 
	 */
	public void checkReplenishmentNeeded() {
		this.status.checkReplenishmentNeeded(this);
	}
	 
	/**
	 * Check needed replenishment whether completed by it's inventory status.
	 */
	public void checkReplenishmentCompleted() {
		this.status.checkReplenishmentCompleted(this);
	}
	 
	/**
	 * Deliver this orderItem.
	 */
	public void deliver() {
		this.status.deliver(this);
	}
	
	/**
	 * Cancel this orderItem. 
	 */
	public void cancel(){
		this.status.cancel(this);
	}
	 
	/**
	 * Get corresponding ordered book.
	 * @return
	 */
	public Book getOrderedBook() {
		return this.orderedBook;
	}
	 
	/**
	 * Get status.
	 * @return
	 */
	public OrderItemStatus getStatus() {
		return this.status;
	}

	/**
	 * @return the quantity
	 */
	public int getQuantity() {
		return quantity;
	}

	/**
	 * @param quantity the quantity to set
	 */
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	/**
	 * @return the unitPrice
	 */
	public BigDecimal getUnitPrice() {
		return unitPrice;
	}

	/**
	 * @param unitPrice the unitPrice to set
	 */
	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}
	
	/**
	 * Get total unit price.
	 * totalUnitPrice = unitPrice * quantity.
	 * @return
	 */
	public BigDecimal getTotalUnitPrice(){
		return this.unitPrice.multiply(new BigDecimal(this.quantity));
	}

	/**
	 * @return the updateTime
	 */
	public Date getUpdateTime() {
		return updateTime;
	}

	/**
	 * @param updateTime the updateTime to set
	 */
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	/**
	 * @param orderedBook the orderedBook to set
	 */
	public void setOrderedBook(Book orderedBook) {
		this.orderedBook = orderedBook;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null) { return false; }
		   if (obj == this) { return true; }
		   if (obj.getClass() != getClass()) {
		     return false;
		   }
		   OrderItem rhs = (OrderItem) obj;
		   return new EqualsBuilder()
			   .append(this.id, rhs.id)
			   .append(this.sequence, rhs.sequence)
			   .append(this.status, rhs.status)
			   .append(this.quantity, rhs.quantity)
			   .append(this.unitPrice, rhs.unitPrice)
			   .append(this.updateTime, rhs.updateTime)
			   .append(this.orderedBook, rhs.orderedBook)
		       .isEquals();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(23, 29).
	       append(this.id).
	       append(this.sequence).
	       append(this.status).
	       append(this.quantity).
	       append(this.unitPrice).
	       append(this.updateTime).
	       append(this.orderedBook).
	       toHashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
	}

	/**
	 * Set status.
	 * @param status
	 */
	protected void setStatus(OrderItemStatus status) {
		this.status = status;
	}
	 
}
 
